<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-09-16 12:19:17
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;

use App\Repositories\FinanceVoucherRepository;
use App\Traits\AttachmentTrait;
use App\Traits\AutoCodeTrait;
use App\Traits\CommonUserTrait;
use App\Traits\VoucherTrait;

class FinanceAmountodd extends BaseModel
    {
    use CommonUserTrait;
    use AttachmentTrait;
    use AutoCodeTrait;
    use VoucherTrait;
    /**
     * 标识
     *
     * @var string
     */
    protected $ACSign = 'FinanceAmountodd';
    public $timestamps = true;
    public $table = "sys_finance_amountodd";
    protected $guarded = [];

    protected $casts = [];

    public function subject()
        {
        return $this->belongsTo(Subject::class);
        }
    public function company()
        {
        return $this->belongsTo(Company::class);
        }
    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'status_with_finance');
            });
        }
    public function type()
        {
        return $this->belongsTo(FinanceAmountoddType::class);
        }

    public function finance($orders = null)
        {

        $gdata = $orders ?? $this;

        if ($gdata->status_id == 2)
            return $gdata->voucher;
        if ($gdata->status_id != 1) {
            fail("单据非审核状态！，无法财审！");
            }

        if (isEmpty($gdata->summary)) {
            $summary = ($gdata->company->finance_id == 1 ? "应收-" : "应付-") . "抹零 {$gdata->amount}元";
            }
        else
            $summary = '[调账]' . $gdata->summary;

        $details = [];

        $tax_amount = $gdata->tax_id == 1 ? round($gdata->amount - $gdata->amount / (1 + $gdata->tax_rate / 100), 2) : 0;

        if ($gdata->company->finance_id == 1) {
            //借 某科目
            $details[] = FinanceVoucherRepository::mergeVoucher($gdata->subject_id, $summary, $gdata->amount - $tax_amount, 0, $gdata->toArray());
            //贷 应收
            $details[] = FinanceVoucherRepository::mergeVoucher(1122, $summary, 0 - $gdata->amount, 0, $gdata->toArray(), 0, $gdata->tax_id);
            if ($tax_amount > 0)
                $details[] = FinanceVoucherRepository::mergeVoucher(22210105, $summary, $tax_amount, 0, $gdata->toArray());
            }
        else if ($gdata->company->finance_id == -1) {
            //贷 某科目
            $details[] = FinanceVoucherRepository::mergeVoucher($gdata->subject_id, $summary, 0, $gdata->amount - $tax_amount, $gdata->toArray());
            //借 应付
            $details[] = FinanceVoucherRepository::mergeVoucher(2202, $summary, 0, 0 - $gdata->amount, $gdata->toArray(), 0, $gdata->tax_id);
            if ($tax_amount > 0)
                $details[] = FinanceVoucherRepository::mergeVoucher(22210101, $summary, 0, $tax_amount, $gdata->toArray());
            }
        else {
            fail("往来单位，没有确定供应商/客户，请联系管理员");
            }

        $voucher = FinanceVoucherRepository::CreateVoucher($gdata->billdate, 1, $details, $gdata, $gdata->voucher?->id);
        auditSave($gdata, true, true, 2);
        return FinanceVoucherRepository::auditVoucher($voucher);
        }
    }
